package org.linlinjava.litemall.admin.web;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.linlinjava.litemall.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.litemall.admin.util.wxSendMessage;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallAdmin;
import org.linlinjava.litemall.db.domain.LitemallFormSupply;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.domain.LitemallWantBuy;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.linlinjava.litemall.db.service.LitemallWantbuyAuditService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;


@RestController
@RequestMapping("/admin/wantbuyAudit")
@Validated
public class AdminWatbuyAuditController {


    @Autowired
    private LitemallWantbuyAuditService wantbuyAuditService;

    /**
     * 查询审批
     */
    @RequiresPermissions("admin:wantbuyAudit:list")
    @RequiresPermissionsDesc(menu = {"信息审批", "供应信息审批"}, button = "查询")
    @GetMapping("/list")
    public Object list(String approveStatus,String sort,
                       String order, boolean selectAll,
                       String userType,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit
    ) {
        return ResponseUtil.okList(wantbuyAuditService.list(approveStatus,userType,sort,order,page,limit,selectAll));
    }

    /**
     * 通过审批
     */
    @RequiresPermissions("admin:wantbuyAudit:approvalWantbuyAudit")
    @RequiresPermissionsDesc(menu = {"信息审批", "供应信息审批"}, button = "批准审批")
    @PostMapping("/approvalWantbuyAudit")
    @Transactional
    public Object approvalWantbuyAudit(@RequestBody LitemallWantBuy wantbuy) {
        Subject currentUser = SecurityUtils.getSubject();
        LitemallAdmin loginAdmin = (LitemallAdmin) currentUser.getPrincipal();
        wantbuy.setStatus(Byte.valueOf("1"));
        wantbuy.setApproveUserId(loginAdmin.getId());
        wantbuy.setApproveTime(LocalDateTime.now());
        wantbuyAuditService.updateById(wantbuy);
        return ResponseUtil.ok(wantbuy);
    }


    /**
     * 驳回审批
     */
    @RequiresPermissions("admin:wantbuyAudit:rejectedWantbuyAudit")
    @RequiresPermissionsDesc(menu = {"信息审批", "供应信息审批"}, button = "驳回审批")
    @PostMapping("/rejectedWantbuyAudit")
    @Transactional(rollbackFor=Exception.class)
    public Object rejectedWantbuyAudit(@RequestBody LitemallWantBuy wantBuy) {
        Subject currentUser = SecurityUtils.getSubject();
        LitemallAdmin loginAdmin = (LitemallAdmin) currentUser.getPrincipal();
        wantBuy.setStatus(Byte.valueOf("2"));
        wantBuy.setApproveUserId(loginAdmin.getId());
        wantBuy.setApproveTime(LocalDateTime.now());
        wantbuyAuditService.updateById(wantBuy);
        return ResponseUtil.ok(wantBuy);
    }


}
